package com.gitee.wsl.ext.base

const val DEFAULT_FLAG=0x00
const val DEFAULT_BASE_FLAG=0x01
const val DEFAULT_All_FLAG=0xFF

interface FlagAction{

    fun ableFlag(vararg flagValue:Int)

    fun ableFlagArray(flagValue:IntArray)

    fun disableFlag(vararg flagValue:Int)

    fun disableFlagArray(flagValue:IntArray)

    fun hasFlag(flagValue:Int):Boolean

    fun hasNotSet():Boolean

    fun ableAll()

    fun disableAll()

}

open class Flag(var flag:Int = DEFAULT_FLAG):FlagAction {

    override fun ableFlag(vararg flagValue:Int){
        flagValue.forEach {
            flag = flag or it
        }
    }


    override fun ableFlagArray(flagValue:IntArray){
        flagValue.forEach {
            flag = flag or it
        }
    }


    override fun disableFlag(vararg flagValue:Int){
        flagValue.forEach {
            flag=flag and it.inv()
        }
    }

    override fun disableFlagArray(flagValue:IntArray){
        flagValue.forEach {
            flag = flag and it.inv()
        }
    }

    override fun hasFlag(flagValue:Int):Boolean{
        return (flag and flagValue) == flagValue
    }

   override fun hasNotSet():Boolean{
       return hasFlag(DEFAULT_FLAG)
   }

   override fun ableAll(){
       ableFlag(DEFAULT_All_FLAG)
   }

    override fun disableAll(){
        flag = DEFAULT_FLAG
    }

    companion object{
        const val FLAG_1=0x1
        const val FLAG_2=0x2
        const val FLAG_3=0x4
        const val FLAG_4=0x8
        const val FLAG_5=0x10
        const val FLAG_6=0x20
        const val FLAG_7=0x40
        const val FLAG_8=0x80
        const val FLAG_9=0x100
        const val FLAG_10=0x200
        const val FLAG_11=0x400
        const val FLAG_12=0x800
        const val FLAG_13=0x1000
        const val FLAG_14=0x2000
        const val FLAG_15=0x4000
        const val FLAG_16=0x8000
    }
}